#include "WindowButtons.h"
#include "Widgets/ImageWidget.h"
#include "Widgets/Window.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Path/Paths.h"
#include "Widgets/WidgetTree.h"

#include "Asset/EngineDefaultPackage.h"

RWindowButtons::RWindowButtons()
{
    Width=100;
    Height=30;
    Spacing=5.f;

    {
        auto Minimize= NewWidget<RImageWidget>(this);
        auto Image=REngineDefaultPackage::Get()->GetMinimizeIcon(); 
        Minimize->Image=Image;
        Minimize->Width=30.f;
        Minimize->Height=30.f;

        auto Mouse=NewWidget<RMouseWidget>(Minimize);
        Mouse->Anchor.Fill(Minimize);

        Mouse->OnClicked.BindLambda([this,Minimize](const SMouseButtonEvent&)->bool{
            RWindowWeakPtr WindowPtr=GetWidgetTree()->GetWindow();
            WindowPtr->Minimize();

            return true;
        });
        
    }
    
    
    {
        auto Maximize= NewWidget<RImageWidget>(this);
        auto Image=REngineDefaultPackage::Get()->GetMaximizeIcon(); 
        auto RestoreImage=REngineDefaultPackage::Get()->GetRestoreIcon(); 
        Maximize->Image=Image;
        Maximize->Width=30.f;
        Maximize->Height=30.f;

        auto Mouse=NewWidget<RMouseWidget>(Maximize);
        Mouse->Anchor.Fill(Maximize);

        Mouse->OnClicked.BindLambda([this, Maximize,Image,RestoreImage](const SMouseButtonEvent&)->bool{
            
            RWindowWeakPtr WindowPtr=GetWidgetTree()->GetWindow();
            if(WindowPtr->IsMaximized())
            {
                WindowPtr->Restore();
                Maximize->Image=Image;

            }else
            {
                WindowPtr->Maximize();
                Maximize->Image=RestoreImage;
            }

            return true;
        });
    }
    
    {
        auto Close= NewWidget<RImageWidget>(this);
        static auto Image=REngineDefaultPackage::Get()->GetCloseIcon();
        Close->Image=Image;
        Close->Width=30.f;
        Close->Height=30.f;

        auto Mouse=NewWidget<RMouseWidget>(Close);
        Mouse->Anchor.Fill(Close);

        Mouse->OnClicked.BindLambda([this,Close](const SMouseButtonEvent&)->bool{
            
            if(PreClose.IsBound())
            {
                if(!PreClose.Execute())
                {
                    return true;
                }
            }

            RWindowWeakPtr WindowPtr=GetWidgetTree()->GetWindow();
            WindowPtr->Close();

            return true;
        });
    }

}